﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Caching;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;

namespace ProjectTest
{
    public partial class _Default : System.Web.UI.Page
    {
        BLL.TestDataBLL bll = new BLL.TestDataBLL();
        static SqlCacheDependency mysqlDep;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //GetSQLData();
                //PageDataBind();
                dddd.InnerHtml = GetImage();
            }
        }

        private string GetPrameValue(string PrameName, string PrameString)
        {
            Regex r = new Regex(@"(^|,)" + PrameName + "=\"([^,]*)\"(,|$)");
            if (r.IsMatch(PrameString))
            {
                return r.Match(PrameString).Groups[2].Value;
            }
            return string.Empty;
        }

        private string GetImage()
        {
            string result = "<img />";

            string filePath = Server.MapPath("images/dz.jpg");
            byte[] image = File.ReadAllBytes(filePath);
            MemoryStream ms = new MemoryStream(image);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            return result;
        }

        private void PageDataBind()
        {
            string msg = "This is my first Memcached.";
            string[] myServers = LYM.CommonWeb.ConfigHelper.GetConfigString("MyServers").Split('|');
            LYM.CommonWeb.MemCached.MemcachedClient client = LYM.CommonWeb.MemCached.MemcachedClient.GetInstance(myServers);
            object b = client.Get("mystrings");
            if (b == null)
            {
                client.Set("mystrings", msg, TimeSpan.FromHours(1));
                b = msg;
            }
            lab_MSG.Text = b.ToString();
        }

        protected void btn_GetSQL_Click(object sender, EventArgs e)
        {
            if (mysqlDep.HasChanged)
            {
                lab_MSG.Text = "数据库数据发生变化，上次修改时间：" + mysqlDep.UtcLastModified;
            }
            GetSQLData();
        }

        private void GetSQLData()
        {
            if (Cache["MySQLBooks"] == null)
            {
                DataSet ds = bll.GetData();
                mysqlDep = new SqlCacheDependency("testCache", "CacheTable");
                Cache.Add("MySQLBooks", ds, mysqlDep, DateTime.MaxValue, TimeSpan.FromMinutes(20), CacheItemPriority.Normal, null);
            }
            gv_TestData.DataSource = Cache["MySQLBooks"];
            gv_TestData.DataBind();
        }
    }
}
